package CRUD;

// default package
// Generated Apr 21, 2011 3:13:26 PM by Hibernate Tools 3.3.0.GA
import Utility.SessionFactoryUtil;
import xmlMapping.Category;
import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

/**
 * Home object for domain model class Category.
 * @see .Category
 * @author Hibernate Tools
 */
public class CategoryHome {

	private static final Log log = LogFactory.getLog(CategoryHome.class);

        public Session session;

    public CategoryHome() {
           if(session==null|| !session.isOpen())
           
        session=SessionFactoryUtil.getSessionFactory().openSession();
       
    }

	public void persist(Category transientInstance) {
          
                session.beginTransaction();
		log.debug("persisting Category instance");
		try {
			session.persist(transientInstance);
                         session.getTransaction().commit();
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(Category instance) {
          
                session.beginTransaction();
		log.debug("attaching dirty Category instance");
		try {
			session.saveOrUpdate(instance);
                         session.getTransaction().commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Category instance) {
          
                session.beginTransaction();
		log.debug("attaching clean Category instance");
		try {
			session.lock(instance, LockMode.NONE);
                         session.getTransaction().commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(Category persistentInstance) {
		log.debug("deleting Category instance");
              
                session.beginTransaction();
		try {
			session.delete(persistentInstance);
                         session.getTransaction().commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Category merge(Category detachedInstance) {
          
                session.beginTransaction();
		log.debug("merging Category instance");
		try {
			Category result = (Category) session
					.merge(detachedInstance);
			 session.getTransaction().commit();
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Category findById(java.lang.Integer id) {
          
                
		log.debug("getting Category instance with id: " + id);
		try {
			Category instance = (Category) session
					.get(Category.class, id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

        public Category findByName(java.lang.String name) {
          
              
		log.debug("getting Category instance with name: " + name);
		try {
			Criteria crit = session.createCriteria(Category.class);
                        Criterion nameEq = Restrictions.eq("name",name);
                        session.createQuery("from Category c where c.name like :name ").setString("name", name);
                        crit.add(nameEq);
			Category instance = (Category)crit.uniqueResult();
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Category instance) {
          
              
		log.debug("finding Category instance by example");
		try {
			List results = session.createCriteria(
					Category.class).add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

         public List search(String key){
           
                session.beginTransaction();
            Criteria crit=  session.createCriteria(Category.class);
            Criterion name=Restrictions.like("name", key,MatchMode.ANYWHERE);
            crit.add(name);
            return crit.list();
        }

        public List getCategories(){
          
                session.beginTransaction();
            Criteria crit=  session.createCriteria(Category.class);;
            return crit.list();
        }
}
